package com.engine.thread;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.biz.jdbc.Product;
import com.biz.jdbc.ProductDao;
import com.biz.result.ItemGetResult;
import com.engine.QueueInstance;

/**
 * @author liuchao
 * @version $Id: LoadDataThread.java,v 0.1 2012-9-22 下午11:44:08 liuchao Exp $
 */
public class LoadDataThread implements Runnable {

    protected   Log                             log               =           LogFactory.getLog(this.getClass());
    private     ProductDao productDao;
    private Long sleepTime = 1000*10*2L;
    
    public  LoadDataThread( ProductDao productDao){
        this.productDao = productDao;
    }
    
    @Override
    public void run() {
        try{
            while(true){
                List<Product> productList = null;
                for( int i=0;i<100;i++){
                    String tabName = "product_"+i;
                    productList = productDao.getByStatus(tabName, null, 1000);
                    if( null != productList && productList.size() > 0){
                        break;
                    }
                }
                // 启动线程
                for( Product product : productList){
                    GetAPICallable getApiCallable = new GetAPICallable( product.getProductId() );
                    ItemGetResult result = QueueInstance.getInstance().getCacheExec().submit(getApiCallable).get();
//                    if( result.getErrorNO() != null){
//                        sleepTime = 1000*10*6*5L;
                    	//ErrorScene^_^530^_^isv.item-is-delete:invalid-numIid-or-iid^_^c5eed5e31351041ea23bfbb7744f8324^_^null^_^Windows XP^_^2012-09-24 22:16:36^_^ProtocalMustParams:sign=BE9763C25E734D2CFC3F5E12C68DC4CB&timestamp=2012-09-24 22:16:36&v=2.0&app_key=12120462&method=taobao.item.get^_^ProtocalOptParams:sign_method=hmac&partner_id=top-sdk-java-20120520&format=json^_^ApplicationParams:num_iid=15137994401&fields=num_iid,cid, num,price, pic_url,title,desc, nick, detail_url, props_name,props, item_img.url^_^Body:{"error_response":{"code":530,"msg":"Remote service error","sub_code":"isv.item-is-delete:invalid-numIid-or-iid","sub_msg":"该商品已被删除"}}
//                    	2012-09-24 22:16:36,171 INFO [com.engine.thread.LoadDataThread] - -------------------get api error----------------
//                    	if(  )
//                        log.info("-------------------get api error----------------");
//                        break;
//                    }
                }
                log.info("-------------------LoadDataThread end----------------");
                Thread.sleep(sleepTime);
            }
            
        } catch(Exception e){
            
        }
        
        
    }

}
